package com.dmxtechnologies.community.war;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dmxtechnologies.community.ejb.util.Base64Util;
import com.dmxtechnologies.community.pojo.CookieModel;


public abstract class CMSBaseAccessBean {
	
	protected Boolean controll = false; 
	
	protected HttpServletRequest request;
	
	protected Cookie[] cookies;
	
	public void setCookie() throws Exception{
		
		request = (HttpServletRequest) FacesContext.getCurrentInstance()
				.getExternalContext().getRequest();
		CookieModel cookieModel= new CookieModel();
		
		String url = request.getParameter("url");
		String picUrl = request.getParameter("picUrl");
		String title =  request.getParameter("title");
		
		if (FacesContext.getCurrentInstance().getExternalContext().getRequestCharacterEncoding() == null){
			if(!"".equals(title)&&title != null){
				title = new String(title.getBytes("iso-8859-1"), "utf-8");
			}
		}
		
		cookieModel.setPicUrl(picUrl);
		cookieModel.setTitleName(title);
		cookieModel.setUrl(url);
		if (url != null && picUrl != null && title != null) {
			cookies = request.getCookies();
			if(cookies != null){
				for (Cookie cookie : cookies) {
					if (cookie.getName().equals("lastView")) {
						controll = true;
						//去掉重复的浏览记录
						List<String> viewList = new ArrayList<String>();
						List<CookieModel> cookieModels = new ArrayList<>();
						//取cookie value值中的几个 免得cookie值超出
						String cooValue = Base64Util.getBase64String(cookie.getValue());
						String[] vals = cooValue.split(" ");
						String cookieValueBase64 = "";
						for (int i=vals.length-1;i>=0;i--) {
							CookieModel cooModel = new CookieModel();
							String[] values = vals[i].split(",");
							cooModel.setTitleName(values[0]);
							cooModel.setUrl(values[1]);
							cooModel.setPicUrl(values[2]);
							if(!viewList.contains(values[1])){
								viewList.add(values[1]);
								cookieModels.add(cooModel);
							}
						}
						int len = cookieModels.size() > 3 ? 3 : cookieModels.size();
						for(int j=len-1;j>=0;j--){
							cookieValueBase64 += cookieModels.get(j).toString()+" ";
						}
						cookieValueBase64 += cookieModel.toString();
						cookie.setValue(Base64Util.setBase64String(cookieValueBase64));
						cookie.setPath("/");
						cookie.setMaxAge(60 * 60 * 24 * 14);
						((HttpServletResponse) FacesContext.getCurrentInstance()
								.getExternalContext().getResponse()).addCookie(cookie);
					}
				}
			}
			
			if(!controll){
				String cookieValueBase64 = cookieModel.toString();
				Cookie cook = new Cookie("lastView",Base64Util.setBase64String(cookieValueBase64));
				cook.setPath("/");
				cook.setMaxAge(60 * 60 * 24 * 14);
				((HttpServletResponse) FacesContext.getCurrentInstance()
						.getExternalContext().getResponse()).addCookie(cook);
			}
		}
	}
}
